<?xml version="1.0" encoding="windows-1251"?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html lang="ru" xml:lang="ru">

<head>
  <meta http-equiv="Content-type" content="text/html; charset=windows-1251" />
  <title>Basis Test Suite - Basis.DOM</title>

  <style type="text/css">
    @import "style/default/style.css";

    /* */
    BODY
    {
      font-family: Georgia;
      font-size: small;
      _font-size: x-small;
    }
  </style>
  
  <script type="text/javascript"  src="../basis.js"></script>
  <script type="text/javascript"  src="../dom.js"></script>
  <script type="text/javascript"  src="../dom_wrapper.js"></script>
  <script type="text/javascript"  src="test.js"></script>
  <script type="text/javascript"  src="common.js"></script>
</head>

<body>
  <div id="playground" style="display: none"><ul id="n1"><li id="n2">1</li><li id="n3">2<ul id="n4"><li id="n5">3</li><li id="n6">4</li><li id="n7">5<ul id="n8"><li id="n9">6</li></ul>7</li></ul>8</li><li id="n10">9</li></ul></div>

  <script type="text/javascript">
    loadTest((function(){
      var DOM = Basis.DOM;

      var CSS = Basis.CSS;
      var Event = Basis.Event;
      var Tester = Basis.Test.Tester;

      var CookieName;
      var TestProperty;

      var updateCount = 0;

      var pg = DOM.get('playground');
      function resolveNodes(nodes){
        var result = [];
        if (!nodes)
          nodes = [];
        else
          if (nodes.constructor != Array)
            nodes = [nodes];
        for (var i = 0; i < nodes.length; i++)
          result.push(nodes[i].id || nodes[i].nodeValue);
        return result.join(' ');
      }

      function rs(nodes){
        var result = Array.from(nodes);
        
        for (var i = 0; i < result.length; i++)
          result[i] = result[i].nodeName + (result[i].nodeType == 1 ? (result[i].id ? '#' + result[i].id : '') : '('+result[i].nodeValue+')');
        
        return result.join(' ');
      }

      var a1 = 'n1 n2 1 n3 2 n4 n5 3 n6 4 n7 5 n8 n9 6 7 8 n10 9';
      var a2 = '1 2 3 4 5 6 7 8 9';
      var a3 = 'n1 n2 n3 n4 n5 n6 n7 n8 n9 n10';
      var a4 = 'n1 n10 9 n3 8 n4 n7 7 n8 n9 6 5 n6 4 n5 3 2 n2 1';

      return [
        {
          name: 'TreeWalker',
          testcase: [
            {
              name: 'create',
              test: function(){
                var w = new DOM.TreeWalker(pg);
                this.is(a1, resolveNodes(w.nodes()));
                w.setDirection(DOM.TreeWalker.BACKWARD);
                this.is(a4, resolveNodes(w.nodes()));

                var w = new DOM.TreeWalker(pg, DOM.IS_TEXT_NODE);
                this.is(a2, resolveNodes(w.nodes()));
                this.is(a1, resolveNodes(w.nodes(Function.$true)));
                w.next();
                w.next();
                this.is(a2, resolveNodes(w.nodes()));

                var w = new DOM.TreeWalker(pg);
                this.is(a2, resolveNodes(w.nodes(DOM.IS_TEXT_NODE)));
                this.is(a1, resolveNodes(w.nodes()));
                
                this.is(a3, resolveNodes(new DOM.TreeWalker(pg, DOM.IS_ELEMENT_NODE).nodes()));

                this.is(a4, resolveNodes(new DOM.TreeWalker(pg, null, DOM.TreeWalker.BACKWARD).nodes()));
              }
            },
            {
              name: 'next/prev',
              test: function(){
                var w = new DOM.TreeWalker(pg);
                var r1 = [], r2 = [];
                var node;
                while (node = w.next())
                  r1.push(node);
                while (node = w.prev())
                  r2.push(node);

                this.is(resolveNodes(r1), resolveNodes(r2.reverse()));
              }
            },
            {
              name: 'next',
              test: function(){
                var w = new DOM.TreeWalker(pg);
                this.is('n1', resolveNodes(w.next()));
                this.is('n2', resolveNodes(w.next()));
                this.is(DOM.get('n2'), w.cursor_);
                w.reset();
                this.is(null, w.cursor_);
                this.is('1', resolveNodes(w.next(DOM.IS_TEXT_NODE)));
                this.is(true, w.filter != DOM.IS_TEXT_NODE);
                this.is('2', resolveNodes(w.next(DOM.IS_TEXT_NODE)));
              }
            },
            {
              name: 'next backward',
              test: function(){
                var w = new DOM.TreeWalker(pg, null, DOM.TreeWalker.BACKWARD);
                this.is('n1', resolveNodes(w.next()));
                this.is('n10', resolveNodes(w.next()));
                this.is(DOM.get('n10'), w.cursor_);
                w.reset();
                this.is(null, w.cursor_);
                this.is('9', resolveNodes(w.next(DOM.IS_TEXT_NODE)));
                this.is(true, w.filter != DOM.IS_TEXT_NODE);
                this.is('8', resolveNodes(w.next(DOM.IS_TEXT_NODE)));
              }
            },
            {
              name: 'prev',
              test: function(){
                var w = new DOM.TreeWalker(pg);
                this.is('9', resolveNodes(w.prev()));
                this.is('n10', resolveNodes(w.prev()));
                this.is(DOM.get('n10'), w.cursor_);
                w.reset();
                this.is(null, w.cursor_);
                this.is('9', resolveNodes(w.prev(DOM.IS_TEXT_NODE)));
                this.is(true, w.filter != DOM.IS_TEXT_NODE);
                this.is('8', resolveNodes(w.prev(DOM.IS_TEXT_NODE)));
              }
            },
            {
              name: 'prev backward',
              test: function(){
                var w = new DOM.TreeWalker(pg, null, DOM.TreeWalker.BACKWARD);
                this.is('1', resolveNodes(w.prev()));
                this.is('n2', resolveNodes(w.prev()));
                this.is(DOM.get('n2'), w.cursor_);
                w.reset();
                this.is(null, w.cursor_);
                this.is('1', resolveNodes(w.prev(DOM.IS_TEXT_NODE)));
                this.is(true, w.filter != DOM.IS_TEXT_NODE);
                this.is('2', resolveNodes(w.prev(DOM.IS_TEXT_NODE)));
              }
            },
            {
              name: 'first',
              test: function(){
                var w = new DOM.TreeWalker(pg);
                this.is('n1', resolveNodes(w.first()));
                this.is(DOM.get('n1'), w.cursor_);
                w.next();
                this.is(true, DOM.get('n1') !== w.cursor_);
                this.is('n1', resolveNodes(w.first()));
                
                w.reset();
                this.is(null, w.cursor_);
                this.is('1', resolveNodes(w.first(DOM.IS_TEXT_NODE)));
                this.is(true, w.filter != DOM.IS_TEXT_NODE);
                w.next();
                this.is('1', resolveNodes(w.first(DOM.IS_TEXT_NODE)));
              }
            },
            {
              name: 'first backward',
              test: function(){
                var w = new DOM.TreeWalker(pg, null, DOM.TreeWalker.BACKWARD);
                this.is('n1', resolveNodes(w.first()));
                this.is(DOM.get('n1'), w.cursor_);
                w.next();
                this.is(true, DOM.get('n1') !== w.cursor_);
                this.is('n1', resolveNodes(w.first()));
                
                w.reset();
                this.is(null, w.cursor_);
                this.is('9', resolveNodes(w.first(DOM.IS_TEXT_NODE)));
                this.is(true, w.filter != DOM.IS_TEXT_NODE);
                w.next();
                this.is('9', resolveNodes(w.first(DOM.IS_TEXT_NODE)));
              }
            },
            {
              name: 'last',
              test: function(){
                var w = new DOM.TreeWalker(pg);
                var el;
                this.is('9', resolveNodes(el = w.last()));
                this.is(el, w.cursor_);
                w.next();
                this.is(true, el !== w.cursor_);
                this.is('9', resolveNodes(w.last()));
                
                w.reset();
                this.is(null, w.cursor_);
                this.is('n10', resolveNodes(w.last(DOM.IS_ELEMENT_NODE)));
                this.is(true, w.filter != DOM.IS_ELEMENT_NODE);
                w.next();
                this.is('n10', resolveNodes(w.last(DOM.IS_ELEMENT_NODE)));
              }
            },
            {
              name: 'last backward',
              test: function(){
                var w = new DOM.TreeWalker(pg, null, DOM.TreeWalker.BACKWARD);
                var el;
                this.is('1', resolveNodes(el = w.last()));
                this.is(el, w.cursor_);
                w.next();
                this.is(true, el !== w.cursor_);
                this.is('1', resolveNodes(w.last()));

                w.reset();
                this.is(null, w.cursor_);
                this.is('n2', resolveNodes(w.last(DOM.IS_ELEMENT_NODE)));
                this.is(true, w.filter != DOM.IS_ELEMENT_NODE);
                w.next();
                this.is('n2', resolveNodes(w.last(DOM.IS_ELEMENT_NODE)));
              }
            }
          ]
        },
        {
          name: 'axis',
          testcase: [
            {
              name: 'AXIS_ANCESTOR/AXIS_ANCESTOR_OR_SELF',
              test: function(){
                var root = DOM.get('n6');
                var node = root.parentNode;
                var r1 = [];
                while (node && node != root.document)
                {
                  r1.push(node);
                  node = node.parentNode;
                }

                this.is(r1, DOM.axis(root, DOM.AXIS_ANCESTOR));
                this.is([document.body], DOM.axis(root, DOM.AXIS_ANCESTOR, function(node){ return node.tagName == 'BODY' }));
                                  
                r1.unshift(root);
                this.is(r1, DOM.axis(root, DOM.AXIS_ANCESTOR_OR_SELF));
                this.is([document.body], DOM.axis(root, DOM.AXIS_ANCESTOR_OR_SELF, function(node){ return node.tagName == 'BODY' }));
              }
            },
            {
              name: 'AXIS_CHILD',
              test: function(){
                var root = DOM.get('n1');
                var node = root.firstChild;
                var r1 = [];
                while (node)
                {
                  r1.push(node);
                  node = node.nextSibling;
                }

                this.is(r1, DOM.axis(root, DOM.AXIS_CHILD));
                this.is('n2 n3 n10'.qw().map(DOM.get), DOM.axis(root, DOM.AXIS_CHILD, function(node){ return node.tagName == 'LI' }));

                var root = DOM.get('n3');
                var node = root.firstChild;
                var r1 = [];
                while (node)
                {
                  r1.push(node);
                  node = node.nextSibling;
                }
                this.is(r1, DOM.axis(root, DOM.AXIS_CHILD));
                this.is(r1.filter(DOM.IS_TEXT_NODE), DOM.axis(root, DOM.AXIS_CHILD, DOM.IS_TEXT_NODE));
              }
            },
            {
              name: 'test #2',
              test: function(){
              }
            }
          ]
        },
        {
          name: '...',
          testcase: [
            {
              name: 'test #1',
              test: function(){
              }
            },
            {
              name: 'test #2',
              test: function(){
              }
            }
          ]
        }
      ];

    })());


  </script>
</body>

</html>